/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.exam;

import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: javaseapp - Ex2
 * <p>Powered by webrx On 2021-08-07 11:32:40
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex5 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要翻译内容：");
        String tel = sc.nextLine();
        //String u = String.format("https://dict.naver.com/linedict/zhendict/dict.html#/cnen/search?query=%s", tel);
        String u = "https://ac-dict.naver.com/linedictweb/ac?q=" + tel + "&st=011&r_lt=000&q_enc=UTF-8&r_format=json&r_enc=UTF-8&_t=1628326101102&_callback=jQuery1111005117831810779694_1628325831172&_=1628325831179";
        System.out.println(u);
        var doc = Jsoup.connect(u).timeout(4000).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15").ignoreContentType(true).execute();
        var html = doc.body().toString();
        //System.out.println(html);
        if (html.contains("[\"en\"]")) {
            //英语翻译中文
            Pattern pc = Pattern.compile(" [\u4e00-\u9fa5a-zA-Z ;]+[\u4e00-\u9fa5]");
            Matcher mc = pc.matcher(html);
            if (mc.find()) {
                System.out.println(mc.group());
            }

        } else if (html.contains("[\"zh\"]")) {
            //中文翻译英语
            Pattern pen = Pattern.compile("\\[\"[\u4e00-\u9fa5]+\"\\],\\[\"([a-zA-Z ]+)\"\\],\\[\"(.+?)\"\\]");
            Matcher mm = pen.matcher(html);
            if (mm.find()) {
                System.out.println(mm.group(1));
                System.out.println(mm.group(2));
            }
        }

    }
}
